package slent.iespana.es.visualizacion;

import com.sun.opengl.util.GLUT;
import es.iespana.slent.reaccion.Atomo;
import slent.iespana.es.visualizacion.DibujadorMolecula;
import javax.media.opengl.*;
import javax.swing.JOptionPane;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *Esta clase implementa el dibujo para la molecula NH3
 * 
 * @author slent
 */
public class MoleculaNH3 extends DibujadorMolecula {

    private Atomo atomoH1;
    private Atomo atomoH2;
    private Atomo atomoH3;
    private Atomo atomoH4;
    private Atomo atomoN;
    private boolean isNH4 = false;
    ColorJOGL color;
    private int secondsEspera;

    public MoleculaNH3() {
        desplazX = -0.01;
        desplazY = -0.02;
        desplazZ = -0.03;

        posicionInicial = new Posicion();
    }

    public GLUT getGlut(GLUT glut) {
        return glut;
    }

    public void setGlut(GLUT glut) {
        this.glut = glut;
    }

    public Atomo getAtomoH1() {
        return atomoH1;
    }

    public void setAtomoH1(Atomo atomoH1) {
        this.atomoH1 = atomoH1;
    }

    public Atomo getAtomoH2() {
        return atomoH2;
    }

    public void setAtomoH2(Atomo atomoH2) {
        this.atomoH2 = atomoH2;
    }

    public Atomo getAtomoH3() {
        return atomoH3;
    }

    public void setAtomoH3(Atomo atomoH3) {
        this.atomoH3 = atomoH3;
    }

    public Atomo getAtomoH4() {
        return atomoH4;
    }

    public void setAtomoH4(Atomo atomoH4) {
        this.atomoH4 = atomoH4;
    }

    public Atomo getAtomoN() {
        return atomoN;
    }

    public void setAtomoN(Atomo atomoN) {
        this.atomoN = atomoN;
    }

    public Posicion getPosicionInicial() {
        return posicionInicial;
    }

    public void setPosicionInicial(Posicion posicionInicial) {
        this.posicionInicial = posicionInicial;
    }

    public boolean isIsNH4() {
        return isNH4;
    }

    public void setIsNH4(boolean isNH4) {
        this.isNH4 = isNH4;
    }

    /**
     * Prepara los datos relacionados con el archivo de la reaccion
     */
    public void preparar() {
        this.atomoH1 = atomoN.getAtomo().get(0);
//        this.atomoH2=atomoN.getAtomo().get(1);
//        this.atomoH3=atomoN.getAtomo().get(2);
    }

    public void dibujar(GL gl) {
        if (esperar && !reaccionando) {
            return;
        }
        if (reaccionando) {
            dibujarReaccion(gl);
        }
        if (!isNH4) {
            gl.glPushMatrix();
            //vamos desplazando la molecula por todos lados
            desplazar(gl);

            //atomo central
            gl.glTranslatef(0.0f, 0.0f, 0.1171f);
            color = ColorJOGL.transformarColor(atomoN.getColor());
//        System.out.println("Color R:"+color.getRed()+" G:"+color.getGreen()+" B:"+color.getBlue());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoN.getRadioRelativo())), 20, 20);

            //H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0.9392f, -0.2733f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
//        System.out.println("radioRelativo: "+atomoH1.getRadioRelativo());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //H2
            gl.glPushMatrix();
            gl.glTranslatef(0.8133f, -0.4696f, -0.2733f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //H3
            gl.glPushMatrix();
            gl.glTranslatef(-0.8133f, -0.4696f, -0.2733f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();


            //enlaces

            //centro -> H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(247, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            //centro -> H2
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(-115, 0, 1, 0);
            gl.glRotated(28, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            //centro -> H3
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(115, 0, 1, 0);
            gl.glRotated(28, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            gl.glPopMatrix();
        }
        if (isNH4) {
            gl.glPushMatrix();
            //vamos desplazando la molecula por todos lados
            desplazar(gl);

            //atomo central
            gl.glTranslatef(0.0f, 0.0f, 0.1171f);
            color = ColorJOGL.transformarColor(atomoN.getColor());
//        System.out.println("Color R:"+color.getRed()+" G:"+color.getGreen()+" B:"+color.getBlue());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoN.getRadioRelativo())), 20, 20);

            //H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0.9392f, -0.2733f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
//        System.out.println("radioRelativo: "+atomoH1.getRadioRelativo());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //H2
            gl.glPushMatrix();
            gl.glTranslatef(0.8133f, -0.4696f, -0.2733f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //H3
            gl.glPushMatrix();
            gl.glTranslatef(-0.8133f, -0.4696f, -0.2733f);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //H4
            gl.glPushMatrix();
            gl.glTranslated(0, 0, 0.9392);
            color = ColorJOGL.transformarColor(atomoH1.getColor());
            gl.glColor3d(color.getRed(), color.getGreen(), color.getBlue());
            glut.glutSolidSphere(0.35 * (Double.parseDouble(atomoH1.getRadioRelativo())), 20, 20);
            gl.glPopMatrix();

            //enlaces

            //centro -> H1
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(247, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            //centro -> H2
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(-115, 0, 1, 0);
            gl.glRotated(28, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            //centro -> H3
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
            gl.glRotated(115, 0, 1, 0);
            gl.glRotated(28, 1, 0, 0);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            //centro -> H4
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0f, 0.1171f);
//            gl.glRotated(115, 0, 1, 0);
//            gl.glRotated(28, 1, 0, 0);
            gl.glRotated(90, 0, 0, 1);
            gl.glColor3f(0.9f, 0.9f, .9f);
            glut.glutSolidCylinder(0.12f, 0.9392f, 30, 1);
            gl.glPopMatrix();

            gl.glPopMatrix();
        }
    }

    @Override
    public void dibujarReaccion(GL gl) {
        super.dibujarReaccion(gl);
//        try {
//            Thread.sleep(1000);
//        } catch (Exception exc) {
//            System.out.println("No esperó");
//        }
        secondsEspera += 1;
        if (secondsEspera > 3) {
            secondsEspera = 0;
            reaccionando = false;
            isNH4=!isNH4;
        }
    }
}
